﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twc.Deployment.Client.Model;
using Twc.Deployment.Client.Logic.DeploymentActions;

namespace Twc.Deployment.Client.Logic
{
    public class DeployService
    {

        private bool _deployWSP;
        private bool _deploySPBackup;
        private bool _deployWebConfig;

        public DeployService(SiteCollection siteCollection)
        {
            SiteCollection = siteCollection;
        }

        public SiteCollection SiteCollection
        {
            get;
            private set;
        }

        public delegate void OnDeploymentAction_EventHandler(string input, string output);

        public event OnDeploymentAction_EventHandler OnDeploymentAction;

        private IList<BaseDeployAction> deploymentActions;

        public void Deploy()
        {
            int success = 0;

            getDeploymentActions();
            raiseOnDeploymentActionEvent("UnDeploy old solutions, for fresh deployment", "");
            foreach (var deploymentAction in deploymentActions)
            {
                var result = deploymentAction.Undeploy();
                raiseOnDeploymentActionEvent(result.CommandInput, result.CommandOutput);
            }

            raiseOnDeploymentActionEvent("Deploying Solutions", "");
            foreach (var deploymentAction in deploymentActions)
            {
                var result = deploymentAction.Deploy();
                if (result.IsSuccess)
                    success++;

                raiseOnDeploymentActionEvent(result.CommandInput, result.CommandOutput);
            }

            raiseOnDeploymentActionEvent("Solution Deployed Successfuly", "");
            //raiseOnDeploymentActionEvent(String.Format("Success = {0}, Failed = {1}", success, deploymentActions.Count - success), "");

        }

        private void raiseOnDeploymentActionEvent(string input, string output)
        {
            if (OnDeploymentAction != null)
                OnDeploymentAction(input, output);
        }

        private void getDeploymentActions()
        {
            deploymentActions = new List<BaseDeployAction>();
            if (DeploySPBackup)
                deploymentActions.Add(new SPBackupDeployAction(this));
            if (DeployWSP)
                deploymentActions.Add(new WSPDeployAction(this));
        }

        public bool DeployWSP
        {
            get
            {
                return _deployWSP;
            }
            set
            {
                _deployWSP = value;
            }
        }

        public bool DeploySPBackup
        {
            get
            {
                return _deploySPBackup;
            }
            set
            {
                _deploySPBackup = value;
            }
        }

        public bool DeployWebConfig
        {
            get
            {
                return _deployWebConfig;
            }
            set
            {
                _deployWebConfig = value;
            }
        }
    }
}
